﻿using Direct.IDAL.User.Money;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.User;
using DirectSell.Model.Money;
using Direct.IDAL.User.Jurisdiction;
using System.Collections;

namespace DirectSell.BLL.User.Money
{
    /// <summary>
    /// 现金币操作业务逻辑
    /// </summary>
    public class RealMoneyService:UserService
    {
        #region 日志记录
        /// <summary>
        /// 日志记录文件名
        /// </summary>
        protected override string LogFile
        {
            get
            {
                return "申请提现";
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 现金币数据访问层
        /// </summary>
        protected IRealMoneyRepository _realMoney { get; set; }

        /// <summary>
        /// 判断二级密码
        /// </summary>
        protected IModifyInfoRespository _modifyInfoRepository { get; set; }

        public RealMoneyService(IRealMoneyRepository realMoney, IModifyInfoRespository modify)
        {
            this._realMoney = realMoney;
            this._modifyInfoRepository = modify;
        }

        #endregion

        #region 操作
        /// <summary>
        /// 申请现金币操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected async override Task<UserRespCode> OperationAsync<T>(T param)
        {
            var model = param as RealMoneyReq;
            var userinfo = await _modifyInfoRepository.GetInfo(model.UserCode);
            model.BankCode = userinfo.BankCode;
            model.BankName = userinfo.BankName;
            model.CardHolder = userinfo.CardHolder;
            model.UserName = userinfo.UserName;

            if (model == null)
            {
                return UserRespCode.传入参数不正确;
            }

            if (!await _modifyInfoRepository.CheckSecondPassword(model.UserCode, model.SecondPassWord))
            {
                return UserRespCode.二级密码输入错误;
            }

            if (userinfo.RealMoney < model.Amount)
            {
                return UserRespCode.余额不足;
            }
            await LogAsync(model);

            Hashtable hstable = new Hashtable();
            //写入申请表
            _realMoney.Apply(model, ref hstable);
            //写入消费表
            _realMoney.InsertConsumption(model,ref hstable);
            //扣费
            _realMoney.Deductions(model, ref hstable);
            //提交事务
            if (!_realMoney.CommitDapperTranLock(hstable))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;

        }
        #endregion

    }
}
